//
//  LoginViewController.m
//  StockNet
//
//  Created by 小焘无量 on 2023/1/5.
//

#import "LoginViewController.h"
#import "CountDownButton.h"


@interface LoginViewController ()

@property (nonatomic, assign) BOOL showClose;
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet CountDownButton *countDownBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController

- (instancetype)initWithShowClose:(BOOL)showClose {
    if (self = [super init]) {
        _showClose = showClose;
    }
    return self;
}

#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self UIConfig];
}

-(void)dealloc {
    //[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - view layout
-(void)UIConfig {
    
    self.countDownBtn.layer.borderColor = kThemeColor.CGColor;
    [self.countDownBtn setTitleColor:kThemeColor forState:UIControlStateNormal];
    self.loginBtn.backgroundColor = kThemeColor;
    self.closeBtn.hidden = !self.showClose;
    [self.navigationController setNavigationBarHidden:YES];
}

#pragma mark - Event Response

- (IBAction)closeAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 获取验证码
- (IBAction)getCodeAction:(CountDownButton *)sender {
    
    if (IsEmptyString(self.phoneTF.text)) {
        [MBProgressHUD showMessage:@"请填写手机号"];
        return;
    }
    
    if (![self.phoneTF.text valiMobile]) {
        [MBProgressHUD showMessage:@"请输入正确的手机号"];
        return;
    }
    
    WeakSelf
    [self.countDownBtn addToucheHandler:sender hander:^(CountDownButton * _Nonnull countDownButton, NSInteger tag) {
        
        // 发送验证码
        [weakSelf getCodeNetwork:sender];
        
        [sender didChange:^NSString *(CountDownButton * _Nonnull countDownButton, int second) {
            NSString *title = [NSString stringWithFormat:@"剩余%02ds",second];
            return title;
        }];
        [sender didFinished:^NSString *(CountDownButton * _Nonnull countDownButton, int second) {
            countDownButton.enabled = YES;
            return @"重新获取";
        }];
    }];
}

// 立即登录
- (IBAction)loginNowAction:(id)sender {
    
//    if (!self.agreeBtn.selected) {
//        [MBProgressHUD showMessage:@"请阅读并同意用户协议和隐私政策"];
//        return;
//    }
    
    if (![self.phoneTF.text valiMobile]) {
        [MBProgressHUD showMessage:@"请输入正确的手机号"];
        return;
    }
    
    if (![self.codeTF.text isNotBlank]) {
        [MBProgressHUD showMessage:@"请输入验证码"];
        return;
    }
    
    [self LoginNetwork];
}

#pragma mark - Delegate,DataSource

#pragma mark - Network
// 发送验证码
-(void)getCodeNetwork:(CountDownButton *)btn {
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:self.phoneTF.text forKey:@"phone"];

    WeakSelf;
    [[APIManager manager] Get:URL_SendLoginCode parameters:dic success:^(id  _Nonnull responseObject) {
        if (NET_RESULT_CODE == 200) {
            btn.enabled = NO;
            [btn startWithSecond:60];//开启计时器
            [MBProgressHUD showMessage:@"获取验证码成功"];
            [weakSelf.codeTF becomeFirstResponder];
        }else {
            NET_SHOW_MSG;
        }
    } failure:^(NSError * _Nonnull error) {
        NET_SHOW_ERROR;
    }];
}

// 登录
-(void)LoginNetwork {
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:self.codeTF.text forKey:@"code"];
    [dic setObject:self.phoneTF.text forKey:@"username"];
    
    
    WeakSelf
    [[APIManager manager] Post:URL_Login parameters:dic success:^(id  _Nonnull responseObject) {
        if (NET_RESULT_CODE == 200) {

            [[YTClient shared] loginInWithData:responseObject];
            [MBProgressHUD showMessage:@"登录成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:NotifiName_LoginStateChange object:@{@"loginIn":@"1"}];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf dismissController];
            });
        }else {
            NET_SHOW_MSG;
        }
    } failure:^(NSError * _Nonnull error) {
        NET_SHOW_ERROR;
    }];
}


#pragma mark - Method

#pragma mark - Set Get

#pragma mark - Lazy



@end
